package com.ruoyi.pay.manager.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.basic.service.impl.BaseServiceImpl;
import com.param.concat.IUpdatePaymentParam;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.pay.manager.mapper.PayManagerMapper;
import com.ruoyi.pay.manager.domain.PayManager;
import com.ruoyi.pay.manager.service.IPayManagerService;
import com.translate.mapper.IBaseMapper;

/**
 * 支付管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-22
 */
@Service
public class PayManagerServiceImpl extends BaseServiceImpl<Long, PayManager> implements IPayManagerService {
	
	public PayManagerServiceImpl(PayManagerMapper baseMapper) {
		super(baseMapper);
		// TODO Auto-generated constructor stub
	}
	
	


	/**
	 * 查询支付管理
	 * 
	 * @param id 支付管理主键
	 * @return 支付管理
	 */
	@Override
	public PayManager selectById(Integer id) {
		return baseMapper.selectById(id);
	}

	/**
	 * 查询支付管理列表
	 * 
	 * @param payManager 支付管理
	 * @return 支付管理
	 */
	@Override
	public List<PayManager> selectList(PayManager payManager) {
		return baseMapper.selectList(payManager);
	}

	/**
	 * 新增支付管理
	 * 
	 * @param payManager 支付管理
	 * @return 结果
	 */
	@Override
	public int insert(PayManager payManager) {
		return baseMapper.insert(payManager);
	}

	/**
	 * 修改支付管理
	 * 
	 * @param payManager 支付管理
	 * @return 结果
	 */
	@Override
	public int update(PayManager payManager) {
		return baseMapper.update(payManager);
	}

	/**
	 * 批量删除支付管理
	 * 
	 * @param ids 需要删除的支付管理主键
	 * @return 结果
	 */
	@Override
	public int deleteByIds(Integer[] ids) {
		return baseMapper.deleteByIds(ids);
	}

	/**
	 * 删除支付管理信息
	 * 
	 * @param id 支付管理主键
	 * @return 结果
	 */
	@Override
	public int deleteById(Integer id) {
		return baseMapper.deleteById(id);
	}

	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public <VO> int updatePayStatus(VO payManager) {
		return baseMapper.updatePayStatus(payManager);
	}


	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int updateTeamId(int teamId, int userId) {
		
		PayManagerMapper payManagerMapper = this.getBaseMapper();
		
		return payManagerMapper.updateTeamId(teamId, userId);
	}

	@Transactional(propagation = Propagation.MANDATORY)
	@Override
	public int updatePrice(
			int payManagerId, 
			BigDecimal price, 
			boolean cancelledByPayManager,
			String serviceName
	) {
		
		PayManagerMapper payManagerMapper = this.getBaseMapper();
		
		PayManager selectById = this.selectById(payManagerId);
		
		if (null == selectById) {
            throw new IllegalArgumentException("找不到支付管理");
        }
		
		String serviceNames = selectById.getServiceNames();
		
	
		String[] split = serviceNames.split("\\+");
		
		StringBuilder res = new StringBuilder();
		
		for (int i = 0; i < split.length; i++) {
			if (serviceName.equals(split[i])) {
				continue;
			}
			
			res = res.append(split[i]).append("+");
		}
		
		int length = res.length();
		
		if (length == 0) {
			return payManagerMapper.updatePrice(payManagerId, price, cancelledByPayManager, "");
		}
		
		String substring = res.substring(0, length -1);
		
		return payManagerMapper.updatePrice(payManagerId, price, cancelledByPayManager, substring);
	}


	@Override
	public BigDecimal sumPriceByTeam(Long teamId) {
		PayManagerMapper payManagerMapper = this.getBaseMapper();
		BigDecimal sumPriceByTeam = payManagerMapper.sumPriceByTeam(teamId);
		
		if (null == sumPriceByTeam) {
			sumPriceByTeam = BigDecimal.ZERO;
		}
		
		return sumPriceByTeam;
	}


	@Override
	public List<PayManager> selectListByBetweenTeamId(long[] ids) {

		PayManagerMapper payManagerMapper = this.getBaseMapper();

		
		return payManagerMapper.selectListByBetweenTeamId(ids);
	}




	@Override
	public int updatePrice(int payManagerId, BigDecimal price) {
		PayManagerMapper payManagerMapper = this.getBaseMapper();
		return payManagerMapper.updatePriceData(payManagerId, price);
	}

}
